package milktea.servlet.user;

import milktea.entity.USER;
import milktea.service.USERService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/modify_userInfo")
public class DoUserModify extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
//        response.setCharacterEncoding("UTF-8");
        String username = request.getParameter("userName");

        String name = request.getParameter("name");
        String psd = request.getParameter("passWord");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String email = request.getParameter("email");
        String mobile = request.getParameter("mobile");
        String idcard = request.getParameter("idcard");
        String address = request.getParameter("address");
        int status = 1;
        //�����û�ʵ��
        USER user = new USER(username, name, psd, sex, birthday, idcard, email, mobile, address, status);


        int count = USERService.update(user);
        System.out.println("count:"+count+"--"+user.getUSER_ADDRESS());
        //�ɹ���ʧ�ܵ�ȥ��

            PrintWriter out = response.getWriter();
            out.write("<script>");
            if (count > 0){
                out.write("alert('用户信息变化,请重新登录');");
                HttpSession session = request.getSession();
                session.removeAttribute("name");
                session.removeAttribute("isLogin");
            }else{
                out.write("alert('用户登录失败');");
            }
            out.write("location.href='login.jsp';");
            out.write("</script>");
            out.close();
    }
}
